package com.fc.redis.service;

import com.fc.redis.entity.TbUser;
import com.fc.redis.entity.TbUserExample;
import com.fc.redis.mapper.TbUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @program: fc-redis->UserService
 * @description: userService
 * @author: G_Y
 * @create: 2019-09-05 16:46
 **/
@Service
@Slf4j
// 注意定义的cacheManager中可以根据cacheName 设置缓存属性 ，具体见RedisConfig
@CacheConfig(cacheManager = "redisCacheManager", cacheNames = {"user", "product"}) // 实现队列  或者二级缓存
public class UserService {
    //condition = "可加缓存条件" // 比如判断冷热数据  近一个月数据缓存 用户经常查询
    @Cacheable(key = "#id")
    public TbUser getUserById(Integer id) {
        log.info("Redis not use, Query Mysql!!");
//        System.out.println("Redis not use, Query Mysql!!");
        return userMapper.selectByPrimaryKey(id);
    }
    @CachePut(key = "#user.id")
    public TbUser updateUserById(TbUser user) {
        user.setUpdateTime(new Date());
        userMapper.updateByPrimaryKeySelective(user);
        return user;
    }
    @Async
    public void testAsyn() {
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    @Transactional
    @Async
    @Cacheable(key = "#user.id")
    public TbUser addUsersAsync(TbUser user) {
//        tbUserMapper.insert(new TbUser("传智播客"));
//        int i = 1/0;
//        tbUserMapper.insert(new TbUser("黑马程序员"));
        return null;
    }
    @Transactional
    public void addUsers(TbUser user) {
//        tbUserMapper.insert(new TbUser("传智播客"));
//        int i = 1/0;
//        tbUserMapper.insert(new TbUser("黑马程序员"));
    }


    // allEntries：
    //是否清空所有缓存内容，缺省为 false，如果指定为 true，则方法调用后将立即清空所有缓存
    //beforeInvocation：
    //是否在方法执行前就清空，缺省为 false，如果指定为 true，则在方法还没有执行的时候就清空缓存，缺省情况下，如果方法执行抛出异常，则不会清空缓存
    @CacheEvict(key = "#id")
    public boolean deleteUserById(Integer id) {
        int i = userMapper.deleteByPrimaryKey(id);
        if (i > 0)
            return true;
        return false;
    }

    private String name = "userService";
    @Autowired
    private TbUserMapper userMapper;

    @Override
    public String toString() {
        return "UserService{" +
                "name='" + name + '\'' +
                '}';
    }

    public List<TbUser> getAll() {
        List<TbUser> tbUsers = userMapper.selectByExample(new TbUserExample());
        return tbUsers;
    }


}
